package com.laob.laob_special.common;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URISyntaxException;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName laob_special
 * @title     ControllerExceptionAdvice   
 * @package    com.laob.laob_special.common  
 * @date   2019/9/25 10:21  
 * @explain 全局异常统一处理类
 */
@ControllerAdvice
public class ControllerExceptionAdvice {


    /**
     * 异常统一处理的方法
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public JSONObject errorHandler(Exception ex){
        JSONObject jsonObject = new JSONObject();
        // 如果是请求地址有误
        if (ex instanceof IllegalArgumentException){
            jsonObject.put("content","请检查IP输入是否有误,正确格式例如: http://51.159.3.194:8899");
        }else {
            jsonObject.put("content","error: "+ex.getMessage());
        }

        return jsonObject;
    }

}
